package cc.cb21;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import oracle.to.mysql.utils.SqlUtils;

public class CB21_Compare_ACB210 {

	private static Logger log = Logger.getLogger(" ");
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("驱动加载异常：" + e.getMessage());
		}
	}
	private static final String ORACLE_SQL="select acb210,acb217 from cb21_cc where acb200 in(select acb200 from cb20_cc where aab001 in (select aab001 from ab01_cc))";
	private static final String MYSQL_SQL="select count(1) from cb21 where acb210='{acb210}'";
	
	public static void main(String[] args) throws SQLException, IOException {
		long time = System.currentTimeMillis();
		Connection connOracle = DriverManager.getConnection(SqlUtils.ORACLE_URL, SqlUtils.ORACLE_USER, SqlUtils.ORACLE_PWD);
		Statement csOracle = connOracle.createStatement();
		ResultSet rs = csOracle.executeQuery(ORACLE_SQL);
		Connection connMysql = DriverManager.getConnection(SqlUtils.getMySqlUrl());
		int index=0;
		while (rs.next()) {
			String acb210 = rs.getString("acb210");
			String acb217 = rs.getString("acb217");
			int count = getCountByID(connMysql, acb210);
			if (count == 0) {
				log.error("mysql中不存在此数据，acb210=" + acb210 + ",acb217=" + acb217);
			} else if (count == 1) {
				//正常
			} else {
				log.warn("mysql中存在" + count + "条数据,acb210=" + acb210 + ",acb217=" + acb217);
			}
			index++;
			if(index%1000 == 0){
				log.info("已对比index="+index);
			}
		}
		log.info("已对比index="+index);
		connOracle.close();
		connMysql.close();
		log.info("按主键，全部对比完成，耗时:" + (System.currentTimeMillis() - time) + "ms");
	}

	private static int getCountByID(Connection conn, String acb210) throws SQLException {
		String sql = MYSQL_SQL.replace("{acb210}", acb210);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		int count = 0;
		if (rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		stmt.close();
		return count;
	}
	
}